﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bll;
using Model;
namespace WebPages.AdminPages
{
    public partial class UserMg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {               
                Databind();
                RepUser.Visible = true;
            }
        }

        protected void RepUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            BllLayer bll = new BllLayer();
            if (e.CommandName == "Edit")
            {
                RepUser.Visible = false;
                edit.Style["visibility"] = "visible";
                User_info user = bll.GetUserInfo(e.CommandArgument.ToString());
                SIDlb.Text = user.SID;
                nametxt.Text = user.name;
                accounttxt.Text = user.account;
                pwdtxt.Text = user.pwd;
                goodtxt.Text = user.goods;
                emailtxt.Text = user.email;
                wallettxt.Text = user.wallet.ToString();
                _ = (user.is_accepted == 0) ? is_acceptedck.Checked = false : is_acceptedck.Checked = true;
                faceimg.Src = user.face;
                timelb.Text = user.regtime.ToString();

            }
        }
        protected void Databind()
        {
            BllLayer bll = new BllLayer();
            RepUser.DataSource = bll.GetUserInfos();
            RepUser.DataBind();
        }

        protected void Accept_Click(object sender, EventArgs e)
        {
            bool isupdate = true;
            BllLayer bll = new BllLayer();
            User_info user = bll.GetUserInfo(SIDlb.Text.ToString());
            user.name = nametxt.Text;
            user.account = accounttxt.Text;
            user.pwd = pwdtxt.Text;
            user.goods = goodtxt.Text;
            user.wallet = float.Parse(wallettxt.Text);
            user.is_accepted = is_acceptedck.Checked?1:0;
            user.email = emailtxt.Text;
            string ext = "";
            if (FileUpload1.HasFile)
            {
                user.face = "..//face//"+user.SID+ Path.GetExtension(FileUpload1.FileName);
                ext = Path.GetExtension(FileUpload1.FileName);
            }
            ext = ext.ToLower();
            if(user.name == "")
            {
                Response.Write("<script>alert('姓名不能为空！')</script>");
                return;
            }
            if(user.account == "")
            {
                Response.Write("<script>alert('账号不能为空！')</script>");
                accounttxt.Focus();
                return;
            }
            if(!System.Text.RegularExpressions.Regex.IsMatch(user.email, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
            {
                Response.Write("<script>alert('邮箱格式错误！')</script>");
                emailtxt.Focus();
                return;
            }
            if(ext != ".jpg" && ext != ".png" && ext != ".jpeg" && ext != ".gif" && ext!="" )
            {
                Response.Write("<script>alert('头像文件格式不合法！')</script>");
                FileUpload1.Focus();
                return;
            }
            if(ext != "")
            {
                try
                {
                    FileUpload1.SaveAs(user.face);
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('保存文件失败！')</script>");
                    FileUpload1.Focus();
                    return;
                }

            }
            if (bll.Update(user) == 1)
            {
                Response.Write("<script>alert('操作成功！')</script>");
                edit.Style["visibility"] = "hidden";
                Databind();
                RepUser.Visible = true;
                return;
            }
            else
            {
                Response.Write("<script>alert('操作失败！')</script>");
            }
            Response.Write("<script>alert('Error！')</script>");

        }

        protected void Cancel_Click(object sender, EventArgs e)
        {

        }
    }
}